<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-03-01
 * Time: 11:49
 */

namespace Admin\Api;


use Admin\Model\VUserInfoModel;
use Common\Api\Api;

class VUserInfoApi extends Api
{
    public function _init()
    {
        $this->model = new VUserInfoModel();
    }

    public function query($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
        if(!array_key_exists("u_group",$map)){
            return parent::query($map,$page,$order,$params,$fields);
        }$u_group = $map['u_group'];
        unset($map['u_group']);
        $map['aga.group_id'] = $u_group;
        $query = $this->model;
        $query->alias("vuser")
            ->field("vuser.*")
            ->join('LEFT JOIN common_auth_group_access as aga ON aga.uid = vuser.id');

        $query = $query->where($map);

        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
        dump($query->getLastSql());

        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model ->alias("vuser")
            ->field("vuser.*")
            ->join('LEFT JOIN common_auth_group_access as aga ON aga.uid = vuser.id')-> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }
}